package u.can.i.up.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import u.can.i.up.db.mysql.SourceInfo;
import u.can.i.up.db.postgre.CategoryInfo;

public class CategoryUtil {

	public static List<String> getCategoryList(String categoryPath) {
		List<String> categoryList = new ArrayList<String>();
		String line;
		BufferedReader in;
		try {
			in = new BufferedReader(new FileReader(categoryPath));
			while ((line = in.readLine()) != null) {
				categoryList.add(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return categoryList;
	}
	
	public static boolean remove(List<CategoryInfo> categoryList, String string) {
		if (string == null) {
			for (int index = 0; index < categoryList.size(); index++) {
				if (categoryList.get(index) == null) {
					categoryList.remove(index);
					return true;
				}
			}
		} else {
			for (int index = 0; index < categoryList.size(); index++)
				if (string.equals(categoryList.get(index).getCategory())) {
					categoryList.remove(index);
					return true;
				}
		}
		return false;
	}
	
	public static boolean remove(List<String> StringList,  CategoryInfo ci) {
		if (ci == null) {
			for (int index = 0; index < StringList.size(); index++) {
				if (StringList.get(index) == null) {
					StringList.remove(index);
					return true;
				}
			}
		} else {
			for (int index = 0; index < StringList.size(); index++)
				if (ci.getCategory().equals(StringList.get(index))) {
					StringList.remove(index);
					return true;
				}
		}
		return false;
	}

	public static Set<String> clusterCategoryString(List<String> categoryList, List<String> dealedCategoryList,
			List<String> undealedCategoryList) {
		Set<String> clusterSet = new HashSet<String>();

		for (String category : categoryList) {
			// 阅读类
			if (category != null && (category.contains("阅读") || category.contains("阅读") || category.contains("资讯")
					|| category.contains("电子阅读") || category.contains("新闻") || category.contains("电子图书")
					|| category.contains("电子电工") || category.contains("电子书籍") || category.contains("新闻资讯")
					|| category.contains("儿童") || category.contains("报刊") || category.contains("育儿")
					|| category.contains("汽车") || category.contains("都市言情") || category.contains("育儿")
					|| category.contains("漫画") || category.contains("电子书") || category.contains("卡通")
					|| category.contains("杂志") || category.contains("阅后即焚") || category.contains("动漫")
					|| category.contains("故事") || category.contains("魔幻") || category.contains("科幻")
					|| category.contains("英语") || category.contains("仙侠") || category.contains("武侠")
					|| category.contains("搞笑") || category.contains("驾考") || category.contains("儿歌")
					|| category.contains("书籍") || category.contains("笑话") || category.contains("图书")
					|| category.contains("小说"))) {
				String tmpStr = "阅读";
				dealedCategoryList.add(category);
				clusterSet.add(tmpStr);
				undealedCategoryList.remove(category);
			}
			// 主题
			if (category != null && (category.contains("主题") || category.contains("壁纸") || category.contains("字体")
					|| category.contains("动态") || category.contains("美化") || category.contains("桌面")
					|| category.contains("美化手机"))) {
				String tmpStr = "主题";
				dealedCategoryList.add(category);
				clusterSet.add(tmpStr);
				undealedCategoryList.remove(category);
			}
			// 办公
			if (category != null && (category.contains("办公") || category.contains("考试") || category.contains("学习"))) {
				String tmpStr = "办公";
				dealedCategoryList.add(category);
				clusterSet.add(tmpStr);
				undealedCategoryList.remove(category);
			}
			// 生活
			if (category != null && (category.contains("便捷生活") || category.contains("出行必用") || category.contains("日常")
					|| category.contains("健身") || category.contains("词典") || category.contains("出行")
					|| category.contains("地图") || category.contains("减肥") || category.contains("餐饮")
					|| category.contains("服务") || category.contains("美食") || category.contains("导航")
					|| category.contains("美妆日化") || category.contains("健康") || category.contains("教育")
					|| category.contains("查询") || category.contains("旅游") || category.contains("旅行")
					|| category.contains("交通") || category.contains("汽车") || category.contains("酒店")
					|| category.contains("医疗") || category.contains("包装") || category.contains("时钟")
					|| category.contains("票务") || category.contains("日历") || category.contains("日程")
					|| category.contains("闹钟") || category.contains("天气") || category.contains("百科")
					|| category.contains("生活"))) {
				String tmpStr = "生活";
				dealedCategoryList.add(category);
				clusterSet.add(tmpStr);
				undealedCategoryList.remove(category);
			}
			// 娱乐
			if (category != null && (category.contains("娱乐") || category.contains("影音") || category.contains("综艺")
					|| category.contains("体育") || category.contains("运动") || category.contains("影视")
					|| category.contains("真人") || category.contains("明星") || category.contains("艺术")
					|| category.contains("电视") || category.contains("电台") || category.contains("电影")
					|| category.contains("笑话") || category.contains("搞笑") || category.contains("视频")
					|| category.contains("直播"))) {
				String tmpStr = "娱乐";
				dealedCategoryList.add(category);
				clusterSet.add(tmpStr);
				undealedCategoryList.remove(category);
			}
			// 社交
			if (category != null && (category.contains("聊天") || category.contains("社交") || category.contains("社区")
					|| category.contains("微博") || category.contains("视频") || category.contains("交友")
					|| category.contains("汽车") || category.contains("直播"))) {
				String tmpStr = "社交";
				dealedCategoryList.add(category);
				clusterSet.add(tmpStr);
				undealedCategoryList.remove(category);
			}
			// 游戏
			if (category != null && (category.contains("游戏") || category.contains("策略") || category.contains("角色")
					|| category.contains("回合") || category.contains("冒险") || category.contains("模拟")
					|| category.contains("街机") || category.contains("塔防") || category.contains("卡牌")
					|| category.contains("竞速") || category.contains("经营") || category.contains("战略")
					|| category.contains("竞技") || category.contains("动作") || category.contains("格斗")
					|| category.contains("飞行") || category.contains("疯狂跑酷") || category.contains("射击")
					|| category.contains("益智") || category.contains("动作射击") || category.contains("休闲游戏")
					|| category.contains("棋牌") || category.contains("桌游") || category.contains("休闲娱乐")
					|| category.contains("休闲养成") || category.contains("益智,") || category.contains("塔防守卫")
					|| category.contains("宝石消除") || category.contains("角色扮演") || category.contains("扑克棋牌")
					|| category.contains("网络游戏") || category.contains("体育格斗") || category.contains("dota")
					|| category.contains("拼图") || category.contains("赛车") || category.contains("网游")
					|| category.contains("RPG") || category.contains("即时") || category.contains("手游")
					|| category.contains("休闲") || category.contains("养成"))) {
				String tmpStr = "游戏";
				dealedCategoryList.add(category);
				clusterSet.add(tmpStr);
				undealedCategoryList.remove(category);
			}
			// 安全
			if (category != null && (category.contains("安全") || category.contains("性能优化") || category.contains("助手")
					|| category.contains("杀毒"))) {
				String tmpStr = "安全";
				dealedCategoryList.add(category);
				clusterSet.add(tmpStr);
				undealedCategoryList.remove(category);
			}
			// 购物
			if (category != null && (category.contains("购物") || category.contains("网购") || category.contains("鞋")
					|| category.contains("优惠") || category.contains("商业") || category.contains("商务")
					|| category.contains("商城") || category.contains("家纺家装") || category.contains("化工精细")
					|| category.contains("服装内衣") || category.contains("照明安防") || category.contains("冶金钢材")
					|| category.contains("食品农业") || category.contains("日用百货") || category.contains("纺织")
					|| category.contains("数码家电") || category.contains("童装母婴") || category.contains("橡胶塑料")
					|| category.contains("团购") || category.contains("机械五金") || category.contains("包装")
					|| category.contains("美妆") || category.contains("美容配饰") || category.contains("母婴市场")
					|| category.contains("服饰市场") || category.contains("动物植物") || category.contains("票务")
					|| category.contains("折扣"))) {
				String tmpStr = "购物";
				dealedCategoryList.add(category);
				clusterSet.add(tmpStr);
				undealedCategoryList.remove(category);
			}
			// 工具
			if (category != null && (category.contains("工具") || category.contains("应用市场") || category.contains("邮箱")
					|| category.contains("相机") || category.contains("相册") || category.contains("浏览器")
					|| category.contains("音频") || category.contains("音乐") || category.contains("拍照")
					|| category.contains("播放") || category.contains("摄影") || category.contains("邮件")
					|| category.contains("图形") || category.contains("图像") || category.contains("云盘")
					|| category.contains("输入法") || category.contains("照相") || category.contains("拍摄")
					|| category.contains("输入") || category.contains("浏览辅助") || category.contains("录音")
					|| category.contains("短信") || category.contains("通话") || category.contains("图片")
					|| category.contains("安卓") || category.contains("安智") || category.contains("助手")
					|| category.contains("美化") || category.contains("管家") || category.contains("通讯")
					|| category.contains("通信") || category.contains("同步") || category.contains("锁屏")
					|| category.contains("刷机") || category.contains("备份") || category.contains("进程")
					|| category.contains("存储") || category.contains("剪辑") || category.contains("系统")
					|| category.contains("铃声") || category.contains("卸载") || category.contains("效率")
					|| category.contains("文件") || category.contains("文档") || category.contains("多媒体")
					|| category.contains("闹钟") || category.contains("电话") || category.contains("必备软件")
					|| category.contains("辅助") || category.contains("照片"))) {
				String tmpStr = "工具";
				dealedCategoryList.add(category);
				clusterSet.add(tmpStr);
				undealedCategoryList.remove(category);
			}
			// 金融
			if (category != null && (category.contains("金融") || category.contains("股票") || category.contains("理财")
					|| category.contains("票务") || category.contains("财经") || category.contains("财务")
					|| category.contains("投资"))) {
				String tmpStr = "金融";
				dealedCategoryList.add(category);
				clusterSet.add(tmpStr);
				undealedCategoryList.remove(category);
			}

		}
		return clusterSet;
	}

	public static Set<String> clusterCategories(List<SourceInfo> resultList, List<String> undealedCategoryList) {
		Set<String> clusterSet = new HashSet<String>();

		for (SourceInfo source : resultList) {
			String mCategory = "";
			boolean flag = false;
			String category = source.getCategory();
			// 阅读类
			if (category != null && (category.contains("阅读") || category.contains("阅读") || category.contains("资讯")
					|| category.contains("电子阅读") || category.contains("新闻") || category.contains("电子图书")
					|| category.contains("电子电工") || category.contains("电子书籍") || category.contains("新闻资讯")
					|| category.contains("儿童") || category.contains("报刊") || category.contains("育儿")
					|| category.contains("汽车") || category.contains("都市言情") || category.contains("育儿")
					|| category.contains("漫画") || category.contains("电子书") || category.contains("卡通")
					|| category.contains("杂志") || category.contains("阅后即焚") || category.contains("动漫")
					|| category.contains("故事") || category.contains("魔幻") || category.contains("科幻")
					|| category.contains("英语") || category.contains("仙侠") || category.contains("武侠")
					|| category.contains("搞笑") || category.contains("驾考") || category.contains("儿歌")
					|| category.contains("书籍") || category.contains("笑话") || category.contains("图书")
					|| category.contains("小编精选") || category.contains("单行本") || category.contains("小说"))) {
				String tmpStr = "阅读";
				clusterSet.add(tmpStr);
				mCategory += "|" + tmpStr;
				flag = true;
			}
			// 主题
			if (category != null && (category.contains("主题") || category.contains("壁纸") || category.contains("字体")
					|| category.contains("动态") || category.contains("美化") || category.contains("桌面")
					|| category.contains("创意色彩") || category.contains("美化手机"))) {
				String tmpStr = "主题";
				clusterSet.add(tmpStr);
				mCategory += "|" + tmpStr;
				flag = true;
			}
			// 办公
			if (category != null && (category.contains("办公") || category.contains("考试") || category.contains("学习"))) {
				String tmpStr = "办公";
				clusterSet.add(tmpStr);
				mCategory += "|" + tmpStr;
				flag = true;
			}
			// 生活
			if (category != null && (category.contains("便捷生活") || category.contains("出行必用") || category.contains("日常")
					|| category.contains("健身") || category.contains("词典") || category.contains("出行")
					|| category.contains("地图") || category.contains("减肥") || category.contains("餐饮")
					|| category.contains("服务") || category.contains("美食") || category.contains("导航")
					|| category.contains("美妆日化") || category.contains("健康") || category.contains("教育")
					|| category.contains("查询") || category.contains("旅游") || category.contains("旅行")
					|| category.contains("交通") || category.contains("汽车") || category.contains("酒店")
					|| category.contains("医疗") || category.contains("包装") || category.contains("时钟")
					|| category.contains("票务") || category.contains("日历") || category.contains("日程")
					|| category.contains("闹钟") || category.contains("天气") || category.contains("百科")
					|| category.contains("门户") || category.contains("建筑风景") || category.contains("生活"))) {
				String tmpStr = "生活";
				clusterSet.add(tmpStr);
				mCategory += "|" + tmpStr;
				flag = true;
			}
			// 娱乐
			if (category != null && (category.contains("娱乐") || category.contains("影音") || category.contains("综艺")
					|| category.contains("体育") || category.contains("运动") || category.contains("影视")
					|| category.contains("真人") || category.contains("明星") || category.contains("艺术")
					|| category.contains("电视") || category.contains("电台") || category.contains("电影")
					|| category.contains("笑话") || category.contains("搞笑") || category.contains("视频")
					|| category.contains("星座") || category.contains("直播"))) {
				String tmpStr = "娱乐";
				clusterSet.add(tmpStr);
				mCategory += "|" + tmpStr;
				flag = true;
			}
			// 社交
			if (category != null && (category.contains("聊天") || category.contains("社交") || category.contains("社区")
					|| category.contains("微博") || category.contains("视频") || category.contains("交友")
					|| category.contains("汽车") || category.contains("直播"))) {
				String tmpStr = "社交";
				clusterSet.add(tmpStr);
				mCategory += "|" + tmpStr;
				flag = true;
			}
			// 游戏
			if (category != null && (category.contains("游戏") || category.contains("策略") || category.contains("角色")
					|| category.contains("回合") || category.contains("冒险") || category.contains("模拟")
					|| category.contains("街机") || category.contains("塔防") || category.contains("卡牌")
					|| category.contains("竞速") || category.contains("经营") || category.contains("战略")
					|| category.contains("竞技") || category.contains("动作") || category.contains("格斗")
					|| category.contains("飞行") || category.contains("疯狂跑酷") || category.contains("射击")
					|| category.contains("益智") || category.contains("动作射击") || category.contains("休闲游戏")
					|| category.contains("棋牌") || category.contains("桌游") || category.contains("休闲娱乐")
					|| category.contains("休闲养成") || category.contains("益智,") || category.contains("塔防守卫")
					|| category.contains("宝石消除") || category.contains("角色扮演") || category.contains("扑克棋牌")
					|| category.contains("网络游戏") || category.contains("体育格斗") || category.contains("dota")
					|| category.contains("拼图") || category.contains("赛车") || category.contains("网游")
					|| category.contains("RPG") || category.contains("即时") || category.contains("手游")
					|| category.contains("经典消除") || category.contains("休闲") || category.contains("养成"))) {
				String tmpStr = "游戏";
				clusterSet.add(tmpStr);
				mCategory += "|" + tmpStr;
				flag = true;
			}
			// 安全
			if (category != null && (category.contains("安全") || category.contains("性能优化") || category.contains("助手")
					|| category.contains("杀毒"))) {
				String tmpStr = "安全";
				clusterSet.add(tmpStr);
				mCategory += "|" + tmpStr;
				flag = true;
			}
			// 购物
			if (category != null && (category.contains("购物") || category.contains("网购") || category.contains("鞋")
					|| category.contains("优惠") || category.contains("商业") || category.contains("商务")
					|| category.contains("商城") || category.contains("家纺家装") || category.contains("化工精细")
					|| category.contains("服装内衣") || category.contains("照明安防") || category.contains("冶金钢材")
					|| category.contains("食品农业") || category.contains("日用百货") || category.contains("纺织")
					|| category.contains("数码家电") || category.contains("童装母婴") || category.contains("橡胶塑料")
					|| category.contains("团购") || category.contains("机械五金") || category.contains("包装")
					|| category.contains("美妆") || category.contains("美容配饰") || category.contains("母婴市场")
					|| category.contains("服饰市场") || category.contains("动物植物") || category.contains("票务")
					|| category.contains("折扣"))) {
				String tmpStr = "购物";
				clusterSet.add(tmpStr);
				mCategory += "|" + tmpStr;
				flag = true;
			}
			// 工具
			if (category != null && (category.contains("工具") || category.contains("应用市场") || category.contains("邮箱")
					|| category.contains("相机") || category.contains("相册") || category.contains("浏览器")
					|| category.contains("音频") || category.contains("音乐") || category.contains("拍照")
					|| category.contains("播放") || category.contains("摄影") || category.contains("邮件")
					|| category.contains("图形") || category.contains("图像") || category.contains("云盘")
					|| category.contains("输入法") || category.contains("照相") || category.contains("拍摄")
					|| category.contains("输入") || category.contains("浏览辅助") || category.contains("录音")
					|| category.contains("短信") || category.contains("通话") || category.contains("图片")
					|| category.contains("安卓") || category.contains("安智") || category.contains("助手")
					|| category.contains("美化") || category.contains("管家") || category.contains("通讯")
					|| category.contains("通信") || category.contains("同步") || category.contains("锁屏")
					|| category.contains("刷机") || category.contains("备份") || category.contains("进程")
					|| category.contains("存储") || category.contains("剪辑") || category.contains("系统")
					|| category.contains("铃声") || category.contains("卸载") || category.contains("效率")
					|| category.contains("文件") || category.contains("文档") || category.contains("多媒体")
					|| category.contains("窗口部件") || category.contains("闹钟") || category.contains("电话")
					|| category.contains("必备软件") || category.contains("网络浏览") || category.contains("辅助")
					|| category.contains("照片"))) {
				String tmpStr = "工具";
				clusterSet.add(tmpStr);
				mCategory += "|" + tmpStr;
				flag = true;
			}
			// 金融
			if (category != null && (category.contains("金融") || category.contains("股票") || category.contains("理财")
					|| category.contains("票务") || category.contains("财经") || category.contains("财务")
					|| category.contains("投资"))) {
				String tmpStr = "金融";
				clusterSet.add(tmpStr);
				mCategory += "|" + tmpStr;
				flag = true;
			}

			if (!flag) {
				mCategory += "其他";
				clusterSet.add("其他");
				undealedCategoryList.add(category);
			} else {
				mCategory = mCategory.substring(mCategory.indexOf("|") + 1);
			}
			source.setCategory(mCategory);
		}
		return clusterSet;
	}

}
